package acm.广度优先搜索;

import java.util.*;

public class O {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        while (sc.hasNext()) {
            int n=sc.nextInt();
            int m=sc.nextInt();
            int t=sc.nextInt();
            sc.nextLine();
            int sx=0,sy=0;
            int ex=0,ey=0;
            char[][]maze=new char[n][];
            for (int i = 0; i < n; i++) {
                String str = sc.nextLine();
                maze[i]=str.toCharArray();
                int j;
                if ((j=str.indexOf("@"))!=-1){
                    sx=i;
                    sy=j;
                }else if((j=str.indexOf("+"))!=-1){
                    ex=i;
                    ey=j;
                }
            }
            System.out.println(bfs(maze, sx, sy, t));
            System.out.println(dfs(maze, sx, sy, t, -1, -1));
        }
    }
    public static int bfs(char[][] maze,int row,int col,int rest){
        Queue<Node> queue=new LinkedList<>();
        Set<Node> set=new HashSet<>();
        Node curr = new Node(row, col, rest, 0);
        queue.add(curr);
        set.add(curr);
        while (!queue.isEmpty()) {
            curr = queue.poll();
            if (maze[curr.x][curr.y]=='+'){
                return curr.step;
            }
            for (int[] d : directions) {
                Node next = new Node(curr.x + d[0], curr.y + d[1], maze[curr.x][curr.y] == '#' ? curr.rest - 1 : curr.rest, curr.step+1);
                if (next.x>=0&&next.x<maze.length
                        &&next.y>=0&&next.y<maze[0].length
                        &&next.rest>=0
                        &&!set.contains(next)){
                    set.add(next);
                    queue.add(next);
                }
            }
        }
        return -1;
    }
    static class Node{
        int x;
        int y;
        int rest;

        int step;

        public Node(int x, int y, int rest,int step) {
            this.x = x;
            this.y = y;
            this.rest = rest;
            this.step=step;
        }

        /**
         * 不具有自反性
         */
        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Node node = (Node) o;
            return x == node.x && y == node.y && (node.rest<rest||node.step>step);
        }

        @Override
        public int hashCode() {
            return Objects.hash(x, y);
        }
    }
    static int[][] directions={
            {0,-1},
            {0,1},
            {-1,0},
            {1,0},
    };
    /**
     * 以row,col作为起点，剩余rest体力时走到终点所需的时间
     * @return
     */
    public static int dfs(char[][] maze,int row,int col,int rest,int fromX,int fromY){
        if (row<0||row>=maze.length||col<0||col>=maze[0].length){
            return -1;
        }
        if (rest<0||maze[row][col]=='.'){
            return -1;
        }
        if (maze[row][col]=='+'){
            return 0;
        }
        if (maze[row][col]=='#') {
            rest--;
        }
        char temp = maze[row][col];
        maze[row][col]='.';
        int min=-1;
        for (int[] d : directions) {
            if ((row + d[0]==fromX&&col + d[1]==fromY)){
                continue;
            }
            int res = dfs(maze, row + d[0], col + d[1], rest,row,col);
            if ((res!=-1&&res<min)||min==-1){
                min=res;
            }
        }
        maze[row][col]=temp;
        return min==-1?min:min+1;
    }

//    public

    static {
        compare();
    }
    public static void compare(){
        int count =10;
        Random random=new Random();
        System.out.println("start");
        while (count-- > 0) {
            int n = (int) (Math.random() * 8 + 1);
            int m = (int) (Math.random() * 8 + 1);
            int sx = (int) (Math.random() * n);
            int sy = (int) (Math.random() * m);
            int ex = (int) (Math.random() * n);
            int ey = (int) (Math.random() * m);
            int rest=random.nextInt(10);
            char[][] maze=new char[n][m];
            for (char[] chars : maze) {
                for (int i = 0; i < chars.length; i++) {
                    chars[i]=random.nextBoolean()?'*':'#';
                }
            }
            maze[sx][sy]='@';
            maze[ex][ey]='+';
            int bfs = bfs(maze, sx, sy, rest);
            int dfs = dfs(maze, sx, sy, rest,sx,sy);
            if (bfs!=dfs){
                System.out.println("预期："+bfs+"\t实际："+dfs);
                for (char[] chars : maze) {
                    System.out.println(new String(chars));
                }
            }
        }
        System.out.println("end");
    }
}